﻿using System;
using System.Reflection;
using System.Configuration;
using System.Web;

using IDAL;
public sealed class Factory
{
    private static readonly string path = ConfigurationManager.AppSettings["DAL"];
    /// <summary>
    /// 创建工厂  //动态new 一个实例
    /// </summary>
    /// <param name="FactoryName">实例名</param>
    /// <returns></returns>
    public static object Create(string FactoryName)
    {
        try { return CreateObject(path, path + "." + FactoryName); }
        catch { return null; }
    }
    /// <summary>
    /// 使用缓存
    /// </summary>
    /// <param name="path"></param>
    /// <param name="CacheKey"></param>
    /// <returns></returns>
    private static object CreateObject(string path, string CacheKey)
    {
        object objType = DataCache.GetCache(CacheKey);
        if (objType == null)
        {
            try { objType = Assembly.Load(path).CreateInstance(CacheKey); }
            catch { }
        }
        return objType;
    }
    /// <summary>
    /// 不使用缓存
    /// </summary>
    /// <param name="path"></param>
    /// <param name="CacheKey"></param>
    /// <returns></returns>
    private static object CreateObjectNoCache(string path, string CacheKey)
    {
        try { return Assembly.Load(path).CreateInstance(CacheKey); }
        catch { return null; }
    }
}
/// <summary>
/// 缓存相关操作类
/// </summary>
public class DataCache
{
    public DataCache() { }
    /// <summary>
    /// 获取当前应用程序指定的CacheKey的Cache值
    /// </summary>
    /// <param name="CacheKey"></param>
    /// <returns></returns>
    public static object GetCache(string CacheKey)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        return objCache[CacheKey];
    }


    /// <summary>
    /// 将指定类缓存
    /// </summary>
    /// <param name="CacheKey"></param>
    public static void SetCache(string CacheKey, object objObject)
    {
        System.Web.Caching.Cache objCache = HttpRuntime.Cache;
        objCache.Insert(CacheKey, objObject);
    }
}

